package com.ssm.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.service.UserdataService;
import com.ssm.vo.Userdata;

@Controller

@RequestMapping("/Userdata")
public class UserdataController {
	@Autowired
	private UserdataService userdataservice;

	@RequestMapping("/add_userdata")
	public ModelAndView add_userdata(Userdata ud_add) {
		ModelAndView mad_add_userdata=new ModelAndView();
		userdataservice.add_userdatadao(ud_add);
		mad_add_userdata.addObject("userHeadPortrait", ud_add.getUserHeadPortrait());
		mad_add_userdata.addObject("userFullName", ud_add.getUserFullName());
		mad_add_userdata.addObject("userReceivingAddress", ud_add.getUserReceivingAddress());
		mad_add_userdata.addObject("userWeddingDate", ud_add.getUserWeddingDate());
		mad_add_userdata.addObject("userSex", ud_add.getUserSex());
		mad_add_userdata.addObject("userAge", ud_add.getUserAge());
		mad_add_userdata.addObject("userHometown", ud_add.getUserHometown());
		mad_add_userdata.addObject("userPhone", ud_add.getUserPhone());
		mad_add_userdata.addObject("userEmail", ud_add.getUserEmail());
		mad_add_userdata.addObject("userPersonalProfile", ud_add.getUserPersonalProfile());
		mad_add_userdata.addObject("xinxi", "添加成功");
		mad_add_userdata.setViewName("HomePage");
		return mad_add_userdata;	
	}

	/**
	 * 上传
	 * @param file
	 * @param session
	 * @return
	 */
	@RequestMapping("/uploading")
	public ModelAndView uploading(MultipartFile file,HttpSession session){
		//获取文件名
		String names=file.getContentType().split("/")[1];
		//获取时间防止重名
		Date da=new Date();
		String fname=da.getTime()+"."+names;
		//获取项目发送后的路径
		String path=session.getServletContext().getRealPath("/files");
		File f=new File(path, fname);
		try {
			file.transferTo(f);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		ModelAndView mo=new ModelAndView();
		mo.addObject("name", fname);
		mo.addObject("feedback", "上传成功");
		mo.setViewName("Uploading");
		return mo;
	}
	/**
	 * 下载
	 * @param session
	 * @param fname
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/download")
	public ResponseEntity<byte[]>download(HttpSession session,String fname) throws IOException{
		String path=session.getServletContext().getRealPath("/files")+fname;
		File file=new File(path);
		HttpHeaders h=new HttpHeaders();
		try {
			//设置字符集
			String s=new String(fname.getBytes("UTF-8"),"iso-8859-1");
			//头部属性信息
			h.setContentDispositionFormData("attachment", s);
			//设置传输类型
			h.setContentType(MediaType.APPLICATION_OCTET_STREAM);
		} catch (UnsupportedEncodingException e) {	
			e.printStackTrace();
		}
		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),
				h,HttpStatus.CREATED);
	}

}
